package map;

import loadsave.Memento;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;

public class Position {
	
	private int x;
	private int y;
	
	public Position() {
		
		this.x = 0;
		this.y = 0;
	}
	
	public Position(Position pos) {
		this.x = pos.getX();
		this.y = pos.getY();
	}
	
	public Position(int x, int y) {
		
		this.x = x;
		this.y = y;
	}
	
	public int getX() {
		
		return this.x;
	}
	
	public int getY() {
		
		return this.y;
	}
	
	public void add( Position p ) {
		this.x += p.x;
		this.y += p.y;
	}
	
	public void update(Direction d) {
		
		this.x += d.deltaX();
		this.y += d.deltaY();
	}
	
	public Position substract(Position pos) {
		int x = pos.getX();
		int y = pos.getY();
		
		int dx = this.x - x;
		int dy = this.y - y;
		
		return new Position(dx,dy);		
	}
	
	public boolean equals(Object o) {
		
		Position pos = (Position) o;
		
		if(pos.getX() == getX() && pos.getY() == getY()) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public int hashCode() {
		
		return this.toString().hashCode();
	}
	
	public String toString() {
		
		return "Position (" + this.x + ", " + this.y + ")";
	}

	public Memento getMemento() {
		return new Memento() {

			@Override
			public LoadSaveObject getState() {
				LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "Position");
				toReturn.add("x", new LoadSavePrimitive("x", "int", String.valueOf(x)));
				toReturn.add("y", new LoadSavePrimitive("y", "int", String.valueOf(x)));
				return toReturn;
			}

			@Override
			public void restoreState(LoadSaveObject lso) {
				x = Integer.valueOf(lso.get("x").getValue());
				y = Integer.valueOf(lso.get("x").getValue());
			}
			
		};
	}
}
